import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome'
import Users from '../components/Users'
import Rights from '../components/permission/Rights'
import Roles from '../components/permission/Roles'

const routers = [
  {
    path: '/login',
    component: Login
  }, {
    path: '/',
    redirect: '/login'
  }, {
    path: '/home',
    component: Home,
    redirect: '/home/welcome',
    children: [
      {
        path: 'welcome',
        component: Welcome
      },
      {
        path: 'users',
        component: Users
      },
      {
        path: 'roles',
        component: Roles
      },
      {
        path: 'rights',
        component: Rights
      }
    ]
  }
]

Vue.use(VueRouter)
const router = new VueRouter({ routes: routers })
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  if (!window.sessionStorage.getItem('token')) {
    return next('/login')
  }
  next()
})
export default router
